################################################################################
# Joshua C. Fjelstul, Ph.D.
# "How the Chamber System at the CJEU Undermines the Consistency of the 
# Court's Application of EU Law"
# Journal of Law and Courts
# replication code for Figure 5
################################################################################

# install ggminimal package (a ggplot theme)
# devtools::install_github("jfjelstul/ggminimal")

# library
library(tidyverse)
library(ggplot2)
library(ggminimal)
library(patchwork)

##################################################
# use of chambers figure
##################################################

# load data
load("replication-materials/assignments.RData")
load("replication-materials/judges.RData")

##################################################
# judges figure
##################################################

# make judge label
judges$label <- stringr::str_c(judges$judge, " (", judges$member_state_code, ")")

# plot data
plot_data <- assignments %>%
  filter(panel_size == 3 | panel_size == 5) %>%
  group_by(judge) %>%
  summarize(
    mean = mean(successful),
    count = n(),
    start = min(year)
  ) %>%
  filter(count >= 50)

# merge in judge data
plot_data <- left_join(
  plot_data, 
  select(judges, judge, label), 
  by = "judge"
)

# order factor
plot_data$label <- fct_reorder(plot_data$label, plot_data$mean)

# make plot
figure_5 <- ggplot(plot_data) + 
  geom_point(aes(x = mean, y = label, size = count)) + 
  scale_size(range = c(1, 5), limits = c(50, 250), breaks = seq(50, 250, 50), name = "Judgments") + 
  theme_minimal() + 
  titles_minimal(x = "Commission win-rate")

# save plot
ggsave(plot = figure_5, filename = "replication-materials/figure-5.pdf", device = "pdf", height = 12, width = 10, scale = 1.1)

################################################################################
# end R script
################################################################################
